<template>
  <div v-if="dialogValue" class="add-page f-c">
    <BackPage title="事项配置" show-back @back="onPageBack" />
    <div class="user1-add-box">
      <el-tabs v-model="activeName" @tab-click="getNodeList">
        <el-tab-pane label="项目立项阶段" name="1" />
        <el-tab-pane label="规划许可阶段" name="2" />
        <el-tab-pane label="施工许可阶段" name="3" />
        <el-tab-pane label="竣工验收阶段" name="4" />
        <el-tab-pane label="标" name="5" />
      </el-tabs>
      <div v-if="activeName == 5">
        <div class="f-r f-w" style="width: 600px;margin-left: 50px">
          <div style="width: 100%">
            <div class="add" @click="openDialog">
              添加事项
            </div>
          </div>
          <div v-for="(item,index) in nodeList" :key="item.productCode" class="node-card f-r a-c j-b ">
            <div class="node-name f-g">{{ item.productName }}</div>
            <div class="node-btn f-r a-c">
              <i class="el-icon-delete" @click="onDeleteNode(index)" />
            </div>
          </div>
        </div>
        <el-button type="primary" @click="onSave">保存</el-button>
        <el-button @click="dialogValue = false">取 消</el-button>
      </div>
      <el-tabs v-else v-model="activeName2" style="margin-top: 40px" tab-position="left" @tab-click="getNodeList">
        <el-tab-pane label="必经流程" name="1" />
        <el-tab-pane label="可选流程" name="2" />
        <div class="f-r f-w" style="width: 600px;margin-left: 50px">
          <div style="width: 100%">
            <div class="add" @click="openDialog">
              添加事项
            </div>
          </div>
          <div v-for="(item,index) in nodeList" :key="item.productCode" class="node-card f-r a-c j-b ">
            <div class="node-name f-g">{{ item.productName }}</div>
            <div class="node-btn f-r a-c">
              <i class="el-icon-delete" @click="onDeleteNode(index)" />
            </div>
          </div>
        </div>
        <el-button type="primary" @click="onSave">保存</el-button>
        <el-button @click="dialogValue = false">取 消</el-button>
      </el-tabs>
    </div>
    <el-dialog
      title="选择事项"
      :visible.sync="addDialog"
      width="750px"
      append-to-body
    >
      <div style="min-height: 300px">
        <el-form>
          <el-form-item label="事项">
            <el-select
              v-model="checkedNode"
              placeholder="请搜索选择"
              filterable
              multiple
              style="width: calc(100% - 50px)"
            >
              <el-option v-for="item in filterNodeList" :key="item.productCode" :value="item.productCode" :label="item.productName" />
            </el-select>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer" style="text-align: left">
        <el-button @click="addDialog = false">取 消</el-button>
        <el-button type="primary" @click="onSaveNode">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { http_basic_event, http_project_event_template } from '@/api'

export default {
  name: 'Config',
  components: {
  },
  data() {
    return {
      dialogValue: false,
      activeName: '1',
      activeName2: '1',
      nodeList: [],
      templateId: '',
      addDialog: false,
      checkedNode: [],
      options1: []
    }
  },
  computed: {
    filterNodeList() {
      if (this.activeName == 5) {
        return this.options1.filter(el => el.isBidding == 1).filter(el => !this.nodeList.find(el2 => el.productCode === el2.productCode))
      } else {
        return this.options1.filter(el => !this.nodeList.find(el2 => el.productCode === el2.productCode))
      }
    }
  },
  methods: {
    onPageBack() {
      this.$emit('on-success')
      this.dialogValue = false
    },
    open(item) {
      http_basic_event.page({ pageNo: 1, pageSize: 9999 }).then(res => {
        this.options1 = res.data.records
        console.log(this.options1.filter(el => el.isBidding == 1))
      })
      this.templateId = item.id
      this.getNodeList()
      this.dialogValue = true
    },
    // 获取类别下事项数据 根据阶段 1项目立项阶段 2规划许可阶段 3施工许可阶段 4竣工验收阶段
    getNodeList() {
      http_project_event_template.getNodes({
        mainMatterId: this.templateId,
        isMatter: 1, // 1事项所用，2流程所用
        stage: this.activeName,
        process: this.activeName2 // 是否必要
      }).then(res => {
        if (res.data && res.data.length) {
          this.nodeList = res.data[0].productVoList
        } else {
          this.nodeList = []
        }
      }).catch(() => {
        this.nodeList = []
      })
    },
    onDeleteNode(index) {
      this.nodeList.splice(index, 1)
    },
    openDialog() {
      this.checkedNode = []
      this.addDialog = true
    },
    onSaveNode() {
      this.nodeList = [
        ...this.nodeList,
        ...this.checkedNode.map(productCode => {
          return this.options1.find(el2 => el2.productCode === productCode)
        })
      ]
      this.addDialog = false
    },
    onSave() {
      http_project_event_template.updateEvent({
        mainMatterId: this.templateId,
        stage: this.activeName,
        process: this.activeName2, // 是否必要
        productJson: this.nodeList.map(el => el.productCode).join(',')
      }).then(res => {
        this.$message.success(res.msg)
      })
    }
  }
}
</script>

<style scoped lang="scss">
.user1-add-box {
  width: 100%;
  background: #FFFFFF;
  border: 1px solid rgba(185, 185, 185, .3);
  border-radius: 16px;
  padding: 60px;
}
.add-page {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 9;
  background-color: #F6F6F6;
}
.add {
  width: 218px;
  height: 68px;
  border-radius: 8px 8px 8px 8px;
  opacity: 1;
  border: 1px solid #4880FF;
  line-height: 68px;
  font-size: 14px;
  color: #4880FF;
  margin-bottom: 33px;
  text-align: center;
  cursor: pointer;
}
.node-card {
  width: 218px;
  height: 68px;
  background: #4880FF;
  border-radius: 8px 8px 8px 8px;
  opacity: 1;
  padding: 10px;
  margin-bottom: 33px;
  margin-right: 42px;
  .node-name {
    font-size: 14px;
    line-height: 16px;
    text-align: left;
    padding-right: 10px;
    color: #fff;
  }
  .node-btn {
    height: 100%;
    cursor: pointer;
    i {
      color: #ffffff;
      font-size: 18px;
    }
  }
}
</style>
